/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sadhar.sipp.perekamanpos;

import org.sadhar.sipp.perekamanpossubpos.PerekamanPosSubPos;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.sadhar.errhandler.ClassAntiNull;
import org.sadhar.sia.common.ClassConnection;
import org.sadhar.sipp.jenispos.JenisPos;

/**
 *
 * @author hendro
 */
public class PerekamanPosDAOImpl implements PerekamanPosDAO {

    public PerekamanPosDAOImpl() {
        ClassConnection.getTransactionProxyFactoryBean().setTarget(this);
    }

    public List<PerekamanPosSubPos> list() throws Exception {
        List<PerekamanPosSubPos> list = new ArrayList<PerekamanPosSubPos>();
        List<Map> rowsPos = ClassConnection.getJdbc().queryForList("SELECT * FROM ppmk.jenisPos");
        for (Map m : rowsPos) {
            //PerekamanPos perekamanPos = new PerekamanPos();
            JenisPos jenisPos = new JenisPos();
            jenisPos.setKodeJenisPos(ClassAntiNull.AntiNullInt(m.get("kodeJenisPos")));
            jenisPos.setJenisPos(ClassAntiNull.AntiNullString(m.get("jenisPos")));
            jenisPos.setNamaTabelSubPos(ClassAntiNull.AntiNullString(m.get("namaTabelSubPos")));
            //perekamanPos.setJenisPos(jenisPos);
            List<Map> rowsSubPos = ClassConnection.getJdbc().queryForList("SELECT * FROM ppmk." + jenisPos.getNamaTabelSubPos());

            if (jenisPos.getNamaTabelSubPos().equalsIgnoreCase("posHarta")) {
                for (Map mp : rowsSubPos) {
                    PerekamanPosSubPos subPos = new PerekamanPosSubPos();
                    subPos.setKode(ClassAntiNull.AntiNullInt(mp.get("kodePosHarta")));
                    subPos.setNama(ClassAntiNull.AntiNullString(mp.get("namaPosHarta")));
                    subPos.setJenisPos("Harta");
                    subPos.setTabel("posHarta");
                    subPos.setIsMutlak(ClassAntiNull.AntiNullString(mp.get("isMutlak")));
                    //perekamanPos.getSubPoses().add(subPos);
                    list.add(subPos);
                }

            }
            if (jenisPos.getNamaTabelSubPos().equalsIgnoreCase("posPiutang")) {
                for (Map mp : rowsSubPos) {
                    PerekamanPosSubPos subPos = new PerekamanPosSubPos();
                    subPos.setKode(ClassAntiNull.AntiNullInt(mp.get("kodePosPiutang")));
                    subPos.setNama(ClassAntiNull.AntiNullString(mp.get("namaPosPiutang")));
                    subPos.setJenisPos("Piutang");
                    subPos.setTabel("posPiutang");
                    subPos.setIsMutlak(ClassAntiNull.AntiNullString(mp.get("isMutlak")));
                    //perekamanPos.getSubPoses().add(subPos);
                    list.add(subPos);
                }

            }
            if (jenisPos.getNamaTabelSubPos().equalsIgnoreCase("posPendapatan")) {
                for (Map mp : rowsSubPos) {
                    PerekamanPosSubPos subPos = new PerekamanPosSubPos();
                    subPos.setKode(ClassAntiNull.AntiNullInt(mp.get("idPosPendapatan")));
                    subPos.setNama(ClassAntiNull.AntiNullString(mp.get("posPendapatan")));
                    subPos.setIsLainLain(ClassAntiNull.AntiNullString(mp.get("isLainlain")));
                    subPos.setJenisPos("Pendapatan");
                    subPos.setTabel("posPendapatan");
                    subPos.setIsMutlak(ClassAntiNull.AntiNullString(mp.get("isMutlak")));
                    //perekamanPos.getSubPoses().add(subPos);
                    list.add(subPos);
                }

            }
            if (jenisPos.getNamaTabelSubPos().equalsIgnoreCase("posBiaya")) {
                for (Map mp : rowsSubPos) {
                    PerekamanPosSubPos subPos = new PerekamanPosSubPos();
                    subPos.setKode(ClassAntiNull.AntiNullInt(mp.get("kodePosBiaya")));
                    subPos.setNama(ClassAntiNull.AntiNullString(mp.get("namaPosBiaya")));
                    subPos.setJenisPos("Biaya");
                    subPos.setTabel("posBiaya");                    
                    subPos.setKodeKelompoPengurusBiaya(ClassAntiNull.AntiNullInt(mp.get("kodeKelompokPengurusBiaya")));
                    subPos.setIsMutlak(ClassAntiNull.AntiNullString(mp.get("isMutlak")));
                    //perekamanPos.getSubPoses().add(subPos);
                    list.add(subPos);
                }

            }
            if (jenisPos.getNamaTabelSubPos().equalsIgnoreCase("posHutang")) {
                for (Map mp : rowsSubPos) {
                    PerekamanPosSubPos subPos = new PerekamanPosSubPos();
                    subPos.setKode(ClassAntiNull.AntiNullInt(mp.get("kodePosHutang")));
                    subPos.setNama(ClassAntiNull.AntiNullString(mp.get("namaPosHutang")));
                    subPos.setJenisPos("Hutang");
                    subPos.setTabel("posHutang");
                    subPos.setIsMutlak(ClassAntiNull.AntiNullString(mp.get("isMutlak")));
                    //perekamanPos.getSubPoses().add(subPos);
                    list.add(subPos);
                }

            }

            //list.add(list);
        }
        return list;
    }

    public void insert(PerekamanPosSubPos pos, String tabel) throws Exception {
        String sql = "";
        if (tabel.equalsIgnoreCase("posHarta")) {
            sql = "INSERT INTO ppmk.posHarta(namaPosHarta) VALUES(?)";
            ClassConnection.getJdbc().update(sql, new Object[]{pos.getNama()});
        }
        if (tabel.equalsIgnoreCase("posPiutang")) {
            sql = "INSERT INTO ppmk.posPiutang(namaPosPiutang) VALUE(?)";
            ClassConnection.getJdbc().update(sql, new Object[]{pos.getNama()});
        }
        if (tabel.equalsIgnoreCase("posPendapatan")) {
            sql = "INSERT INTO ppmk.posPendapatan(posPendapatan,isLainlain,namaTabelAnggaran,kodeGL) VALUES(?,?,?,?)";
            ClassConnection.getJdbc().update(sql, new Object[]{pos.getNama(), pos.getIsLainLain(),"",""});
        }
        if (tabel.equalsIgnoreCase("posBiaya")) {
            sql = "INSERT INTO ppmk.posBiaya(namaPosBiaya,kodeKelompokPengurusBiaya,isMutlak,namaTabelItem,namaTabelSubItem) VALUES(?,?,?,?,?)";
            ClassConnection.getJdbc().update(sql, new Object[]{pos.getNama(),
                        pos.getKodeKelompoPengurusBiaya(),"N","",""
                    });
        }
        if (tabel.equalsIgnoreCase("posHutang")) {
            sql = "INSERT INTO ppmk.posHutang(namaPosHutang) VALUES(?)";
            ClassConnection.getJdbc().update(sql, new Object[]{pos.getNama()});
        }
    }

    public void update(PerekamanPosSubPos pos, String tabel) throws Exception {
        String sql = "";
        if (tabel.equalsIgnoreCase("posHarta")) {
            sql = "UPDATE ppmk.posHarta SET namaPosHarta=? WHERE kodePosHarta=?";
            ClassConnection.getJdbc().update(sql, new Object[]{pos.getNama(), pos.getKode()});
        }
        if (tabel.equalsIgnoreCase("posPiutang")) {
            sql = "UPDATE ppmk.posPiutang SET namaPosPiutang=? WHERE kodePosPiutang=?";
            ClassConnection.getJdbc().update(sql, new Object[]{pos.getNama(), pos.getKode()});
        }
        if (tabel.equalsIgnoreCase("posPendapatan")) {
            sql = "UPDATE ppmk.posPendapatan SET posPendapatan=?, isLainlain=? WHERE idPosPendapatan=?";
            ClassConnection.getJdbc().update(sql, new Object[]{pos.getNama(), pos.getIsLainLain(), pos.getKode()});
        }
        if (tabel.equalsIgnoreCase("posBiaya")) {
            sql = "UPDATE ppmk.posBiaya SET namaPosBiaya=?, kodeKelompokPengurusBiaya=? WHERE kodePosBiaya=?";
            ClassConnection.getJdbc().update(sql, new Object[]{pos.getNama(),
                        pos.getKodeKelompoPengurusBiaya(), pos.getKode()});
        }
        if (tabel.equalsIgnoreCase("posHutang")) {
            sql = "UPDATE ppmk.posHutang SET namaPosHutang=? WHERE kodePosHutang=?";
            ClassConnection.getJdbc().update(sql, new Object[]{pos.getNama(), pos.getKode()});
        }
    }

    public void delete(PerekamanPosSubPos pos, String tabel) throws Exception {
        String sql = "";
        if (tabel.equalsIgnoreCase("posHarta")) {
            sql = "DELETE FROM ppmk.posHarta WHERE kodePosHarta=?";
            ClassConnection.getJdbc().update(sql, new Object[]{pos.getKode()});
        }
        if (tabel.equalsIgnoreCase("posPiutang")) {
            sql = "DELETE FROM ppmk.posPiutang WHERE kodePosPiutang=?";
            ClassConnection.getJdbc().update(sql, new Object[]{pos.getKode()});
        }
        if (tabel.equalsIgnoreCase("posPendapatan")) {
            sql = "DELETE FROM ppmk.posPendapatan WHERE idPosPendapatan=?";
            ClassConnection.getJdbc().update(sql, new Object[]{pos.getKode()});
        }
        if (tabel.equalsIgnoreCase("posBiaya")) {
            sql = "DELETE FROM ppmk.posBiaya WHERE kodePosBiaya=?";
            ClassConnection.getJdbc().update(sql, new Object[]{pos.getKode()});
        }
        if (tabel.equalsIgnoreCase("posHutang")) {
            sql = "DELETE FROM ppmk.posHutang WHERE kodePosHutang=?";
            ClassConnection.getJdbc().update(sql, new Object[]{pos.getKode()});
        }
    }

    public List select(String nama, String tabel) throws Exception {
        String sql = "";
        List list = new ArrayList();
        if (tabel.equalsIgnoreCase("posHarta")) {
            sql = "SELECT * FROM ppmk.posHarta WHERE namaPosHarta=?";
            list = ClassConnection.getJdbc().queryForList(sql, new Object[]{nama});
        }
        if (tabel.equalsIgnoreCase("posPiutang")) {
            sql = "SELECT * FROM ppmk.posPiutang WHERE namaPosPiutang=?";
            list = ClassConnection.getJdbc().queryForList(sql, new Object[]{nama});
        }
        if (tabel.equalsIgnoreCase("posPendapatan")) {
            sql = "SELECT * FROM ppmk.posPendapatan WHERE posPendapatan=?";
            list = ClassConnection.getJdbc().queryForList(sql, new Object[]{nama});
        }
        if (tabel.equalsIgnoreCase("posBiaya")) {
            sql = "SELECT * FROM ppmk.posBiaya WHERE namaPosBiaya=?";
            list = ClassConnection.getJdbc().queryForList(sql, new Object[]{nama});
        }
        if (tabel.equalsIgnoreCase("posHutang")) {
            sql = "SELECT * FROM ppmk.posHutang WHERE namaPosHutang=?";
            list = ClassConnection.getJdbc().queryForList(sql, new Object[]{nama});
        }
        return list;
    }

    public List select(int kode, String nama, String tabel) throws Exception {
        String sql = "";
        List list = new ArrayList();
        if (tabel.equalsIgnoreCase("posHarta")) {
            sql = "SELECT * FROM ppmk.posHarta WHERE kodePosHarta<>? AND namaPosHarta=?";
            list = ClassConnection.getJdbc().queryForList(sql, new Object[]{kode, nama});
        }
        if (tabel.equalsIgnoreCase("posPiutang")) {
            sql = "SELECT * FROM ppmk.posPiutang WHERE kodePosPiutang<>? AND namaPosPiutang=?";
            list = ClassConnection.getJdbc().queryForList(sql, new Object[]{kode, nama});
        }
        if (tabel.equalsIgnoreCase("posPendapatan")) {
            sql = "SELECT * FROM ppmk.posPendapatan WHERE idPosPendapatan<>? AND posPendapatan=?";
            list = ClassConnection.getJdbc().queryForList(sql, new Object[]{kode, nama});
        }
        if (tabel.equalsIgnoreCase("posBiaya")) {
            sql = "SELECT * FROM ppmk.posBiaya WHERE kodePosBiaya<>? AND namaPosBiaya=?";
            list = ClassConnection.getJdbc().queryForList(sql, new Object[]{kode, nama});
        }
        if (tabel.equalsIgnoreCase("posHutang")) {
            sql = "SELECT * FROM ppmk.posHutang WHERE kodePosHutang<>? AND namaPosHutang=?";
            list = ClassConnection.getJdbc().queryForList(sql, new Object[]{kode, nama});
        }
        return list;
    }
}
